package com.pl.lc.user.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pl.lc.common.config.SystemConfig;
import com.pl.lc.common.param.PayAdd;
import com.pl.lc.common.util.JwtUtil;
import com.pl.lc.common.vo.R;
import com.pl.lc.user.entity.Wallet;
import com.pl.lc.user.mapper.WalletMapper;
import com.pl.lc.user.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.pl.lc.common.vo.R.fail;

/**
 * <p>
 * 6.用户钱包表 前端控制器
 * </p>
 *
 * @author pl
 * @since 2024-06-11
 */
@RestController
@RequestMapping("/user/wallet")
public class WalletController {
    @Autowired
    private WalletService walletService;
    @Autowired
    private WalletMapper walletMapper;

    @RequestMapping("/create")
    public R create(HttpServletRequest request, @RequestBody PayAdd payAdd) {
    //判断参数
        if (payAdd==null){
            return fail("参数为空");
        }
    //    获取userid
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        payAdd.setUserId(id);
    //根据请求参数的不同,选择不同的充值方式
    switch (payAdd.getPayType()){
        case 2:
           return walletService.createWalletByZfb(payAdd);
        case 3:
            return R.fail("暂不支持微信充值");
        default:
            return R.fail("暂不支持其他方式充值");
    }
    }
    @RequestMapping("/my")
    public R my(HttpServletRequest request) {
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        QueryWrapper<Wallet> queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",id);
        Wallet wallet = walletMapper.selectOne(queryWrapper);
        if (wallet!=null){
            return R.ok(wallet);
        }
        return R.fail("查询钱包余额失败");

    }
}

